package com.bindatax.spi.log.log4j;

import com.bindatax.core.log.LogOutInterface;
import com.bindatax.core.log.impl.BaseLogOut;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

public class BinDataxLogerLog4j extends BaseLogOut implements LogOutInterface {
    private final static Map<String, Logger> LOGGER_MAP = new ConcurrentHashMap<>();

    @Override
    public String getType() {
        return TYPE_LOG4J;
    }

    @Override
    public void init(Properties logConfig) {
        super.init(logConfig);
        if (_config.isEmpty()){
            System.out.println("日志配置文件的log4j项为空！");
        }else {
            System.out.println("开始初始化BinDataxLog4j实例。配置项数量：" + _config.size());
            PropertyConfigurator.configure(_config);
        }
    }

    private Logger getLogger(String name) {
        Logger logger4j = LOGGER_MAP.get(name);
        if (logger4j == null) {
            logger4j = Logger.getLogger(name);
            LOGGER_MAP.put(name, logger4j);
        }
        return logger4j;
    }

    @Override
    public void info(String msg) {
        Logger logger = getLogger( getInvokeName(null) );
        logger.info(msg);
    }

    @Override
    public void info(String name, String msg) {
        Logger logger = getLogger( getInvokeName(name) );
        logger.info(msg);
    }

    @Override
    public void info(String msg, Throwable t) {
        Logger logger = getLogger( getInvokeName(null) );
        logger.info(msg, t);
    }

    @Override
    public void info(String name, String msg, Throwable t) {
        Logger logger = getLogger(getInvokeName(name));
        logger.info(msg, t);
    }

    @Override
    public void warn(String msg) {
        Logger logger = getLogger(getInvokeName(null));
        logger.warn(msg);
    }

    @Override
    public void warn(String name, String msg) {
        Logger logger = getLogger(getInvokeName(name));
        logger.warn(msg);
    }

    @Override
    public void warn(String msg, Throwable t) {
        Logger logger = getLogger(getInvokeName(null));
        logger.warn(msg);
    }

    @Override
    public void warn(String name, String msg, Throwable t) {
        Logger logger = getLogger(getInvokeName(name));
        logger.warn(msg);
    }

    @Override
    public void debug(String msg) {
        Logger logger = getLogger(getInvokeName(null));
        logger.debug(msg);
    }

    @Override
    public void debug(String name, String msg) {
        Logger logger = getLogger(getInvokeName(name));
        logger.debug(msg);
    }

    @Override
    public void debug(String msg, Throwable t) {
        Logger logger = getLogger(getInvokeName(null));
        logger.debug(msg, t);
    }

    @Override
    public void debug(String name, String msg, Throwable t) {
        Logger logger = getLogger(getInvokeName(name));
        logger.debug(msg, t);
    }

    @Override
    public void error(String msg) {
        Logger logger = getLogger(getInvokeName(null));
        logger.error(msg);
    }

    @Override
    public void error(String name, String msg) {
        Logger logger = getLogger(getInvokeName(name));
        logger.error(msg);
    }

    @Override
    public void error(String msg, Throwable t) {
        Logger logger = getLogger(getInvokeName(null));
        logger.error(msg, t);
    }

    @Override
    public void error(String name, String msg, Throwable t) {
        Logger logger = getLogger(getInvokeName(name));
        logger.error(msg, t);
    }
}
